<?php

class Node_Model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Insert node
     * @param <array> $nodeParams
     * @param <array> $nodeFieldParams
     * @return <boolean>
     */
    public function insertNode($nodeParams, $nodeFieldParams)
    {
        //Insert nodes
        $this->db->insert('nodes', $nodeParams);

        $nodeFieldParams['node_id'] = $this->db->insert_id();

        //Insert node_fields
        $this->db->insert('node_fields', $nodeFieldParams);

        return $nodeFieldParams['node_id'];
    }

    /**
     * Get node detail by node id
     * @param <int> $nodeID
     * @return <array>
     */
    public function getNodeDetail($nodeID)
    {
        $query = 'SELECT nodes.*,node_fields.*,categories.category_id, categories.title AS category_title ';
        $query.= 'FROM nodes, node_fields, categories ';
        $query.= 'WHERE nodes.node_id = '. $nodeID .' ';
        $query.= 'AND nodes.node_id = node_fields.node_id ';
        $query.= 'AND nodes.category_id = categories.category_id';

        //Call query
        $result = $this->db->query($query)->row_array();

        //Update total view
        $this->db->where('node_id', $nodeID)->update('nodes', array('view' => $result['view']+1));

        return $result;
    }

    /**
     * Delete node by $nodeID
     * @param <int> $nodeID
     * @return <bool>
     */
    public function deleteNode($nodeID)
    {
        //Delete fields of node
        $this->db->delete('node_fields', array('node_id' => $nodeID));

        //Delete node
        $this->db->delete('nodes', array('node_id' => $nodeID));

        //Check if node is deleted and return value
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    /**
     * Get List Node By CategoryID
     * @param <type> $categoryID
     * @param <type> $limit
     * @param <type> $offset
     * @return <type>
     */
    public function getListNodeByCategoryID($categoryID, $limit, $offset)
    {
        $query = 'SELECT SQL_CALC_FOUND_ROWS * FROM nodes ';
        $query.= 'WHERE category_id = '. $categoryID .' ';
        $query.= 'ORDER BY node_id DESC LIMIT '. $offset .', '. $limit;

        $result['data'] = $this->db->query($query)->result_array();

        $query = 'SELECT FOUND_ROWS() AS total';

        $total  = $this->db->query($query)->row_array('total');

        $result['total'] = $total['total'];

        return $result;
    }

    /**
     * Get list node by parentID
     * @param type $parentID
     * @param type $limit
     * @param type $offset
     * @return type 
     */
    public function getListNodeByParentID($parentID, $limit, $offset)
    {
        $query = 'SELECT SQL_CALC_FOUND_ROWS nodes.*,categories.title AS category_title ';
        $query.= 'FROM nodes, categories ';
        $query.= 'WHERE categories.parent_id = '. $parentID .' ';
        $query.= 'AND categories.category_id = nodes.category_id ';
        $query.= 'ORDER BY nodes.node_id DESC LIMIT '. $offset .', '. $limit;

        $result['data'] = $this->db->query($query)->result_array();

        $query = 'SELECT FOUND_ROWS() AS total';

        $total  = $this->db->query($query)->row_array('total');

        $result['total'] = $total['total'];

        return $result;
    }

    /**
     * Update node
     * @param type $data
     * @return type 
     */
    public function editNode($nodeID, $nodeParams, $nodeFieldParams)
    {
        //Update nodes
        $this->db->update('nodes', $nodeParams, array('node_id' => $nodeID));
        
        //Update nodeField
        $result = $this->db->update('node_fields', $nodeFieldParams, array('node_id' => $nodeID));
        
        //Return data
        return $result;
    }

    /**
     * Get list event
     * @param <type> $categoryID
     * @param <type> $limit
     * @param <type> $offset
     * @return <type>
     */
    public function getListEvent($categoryID, $limit, $offset)
    {
        $query = 'SELECT SQL_CALC_FOUND_ROWS * FROM nodes, node_fields ';
        $query.= 'WHERE nodes.category_id = '. $categoryID .' ';
        $query.= 'AND nodes.node_id = node_fields.node_id ';
        $query.= 'ORDER BY nodes.node_id DESC LIMIT '. $offset .', '. $limit;

        $result['data'] = $this->db->query($query)->result_array();

        $query = 'SELECT FOUND_ROWS() AS total';

        $total  = $this->db->query($query)->row_array('total');

        $result['total'] = $total['total'];

        return $result;
    }
    
    /**
     * Get list advertise
     * @param <type> $categoryID
     * @param <type> $limit
     * @param <type> $offset
     * @return <type>
     */
    public function getListAdvertise($categoryID, $limit, $offset)
    {
        $query = 'SELECT SQL_CALC_FOUND_ROWS * FROM nodes, node_fields ';
        $query.= 'WHERE nodes.category_id = '. $categoryID .' ';
        $query.= 'AND nodes.node_id = node_fields.node_id ';
        $query.= 'ORDER BY nodes.node_id DESC LIMIT '. $offset .', '. $limit;

        $result['data'] = $this->db->query($query)->result_array();

        $query = 'SELECT FOUND_ROWS() AS total';

        $total  = $this->db->query($query)->row_array('total');

        $result['total'] = $total['total'];

        return $result;
    }

}

?>